package cn.bincker.mybatis.encrypt;

import cn.bincker.mybatis.encrypt.type.EncryptParam;
import cn.bincker.mybatis.encrypt.type.NoEncryptEncryptParamTypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;

@ConditionalOnMissingClass("cn.bincker.mybatis.encrypt.MybatisEncryptAutoConfiguration")
public class MybatisEncryptParameterAutoConfiguration {
    @Bean
    @ConditionalOnMissingClass("com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer")
    public ConfigurationCustomizer configurationCustomizer(){
        return configuration -> {
            var typeRegistry = configuration.getTypeHandlerRegistry();
            registry(typeRegistry);
        };
    }

    @Bean
    @ConditionalOnClass(name = "com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer")
    public com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer mybatisPlusConfigurationCustomizer(){
        return configuration -> {
            var typeRegistry = configuration.getTypeHandlerRegistry();
            registry(typeRegistry);
        };
    }

    private void registry(TypeHandlerRegistry typeRegistry) {
        typeRegistry.register(EncryptParam.class, new NoEncryptEncryptParamTypeHandler(typeRegistry));
    }
}
